﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Mvc;
using Xenta.Attributes;
using Xenta.Utils;
using Xenta.Web.Controllers;

namespace Xenta.Web.Areas.Wizard.Controllers
{
    [Authenticate]
    public sealed class FileController : ExtController
    {
        #region GET/POST Actions

        [HttpGet, RestoreState]
        public ActionResult Index(int? page)
        {
            var m = Svc.ProcessOperation("SearchFiles", new
            {
                OwnerID = JDM.Int32(Svc.FileOwner.EntityID),
                Index = page * 30,
                Count = 30
            }, new
            {
                DisclosureLevel = "Base"
            });
            return View(m);
        }

        [HttpPost, PersistState]
        public ActionResult Upload(Guid storageID, IEnumerable<HttpPostedFileBase> files)
        {
            foreach(var i in files)
            {
                if(i == null)
                    continue;
                try
                {
                    var tmp = Svc.ProcessOperation("CreateFile", new
                    {
                        OwnerID = JDM.Int32(Svc.FileOwner.EntityID),
                        FolderID = 0,
                        StorageID = storageID,
                        Name = Path.GetFileName(i.FileName),
                        MimeType = i.ContentType,
                        Size = i.ContentLength,
                        Flags = "Public"
                    }, new
                    {
                        DisclosureLevel = "Keys"
                    });
                    Guid fileGuid = JDM.Guid(tmp.Guid);
                    using(var stream = i.InputStream)
                        Svc.UploadFile(fileGuid, stream);
                }
                catch(Exception ex)
                {
                    ModelState.AddModelError("API", ex);
                }
            }
            return RedirectToAction(Loc.Act("Index").Localized());
        }

        [HttpGet, PersistState]
        public ActionResult Delete(int id)
        {
            try
            {
                Svc.ProcessOperation("DeleteFile", new
                {
                    EntityID = id
                });
            }
            catch(Exception ex)
            {
                ModelState.AddModelError("API", ex);
            }
            return RedirectToAction(Loc.Act("Index").Localized());
        }

        #endregion

        #region AJAX Actions

        [HttpPost]
        public ActionResult Autocomplete(string term)
        {
            var data = new List<Object>();
            foreach(var i in Svc.ProcessOperation("SearchFiles", new
            {
                Term = new
                {
                    Expression = String.Format("{0}%", term)
                },
                OwnerID = JDM.Int32(Svc.FileOwner.EntityID),
                Count = 10
            }, new
            {
                DisclosureLevel = "Base"
            }).Items)
            {
                data.Add(new
                {
                    id = JDM.Int32(i.EntityID),
                    value = JDM.String(i.Name)
                });
            }
            return Json(data.ToArray());
        }

        [HttpPost]
        public ActionResult Storages()
        {
            dynamic m = Svc.ProcessOperation("ListFileStorages");
            var data = new List<Object>();

            foreach(var i in m)
            {
                data.Add(new
                {
                    id = JDM.Guid(i.ID),
                    value = JDM.String(i.Name)
                });
            }
            return Json(data.ToArray());
        }

        #endregion
    }
}
